﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Apparat.Renderables.AssimpMeshInterface
{
    public class AssimpCarModel : AssimpModel
    {
        SlimDX.Matrix SteerRotMatrix = SlimDX.Matrix.Identity;
        SlimDX.Matrix RotWheelMatrixFrontLeft = SlimDX.Matrix.Identity;
        SlimDX.Matrix RotWheelMatrixFrontRight = SlimDX.Matrix.Identity;
        SlimDX.Matrix RotWheelMatrixRearLeft = SlimDX.Matrix.Identity;
        SlimDX.Matrix RotWheelMatrixRearRight = SlimDX.Matrix.Identity;


        public override void Render()
        {
            //SteerRotMatrix = SlimDX.Matrix.RotationZ(Kern.Degree.ToRadian(25.0f));

            if (AssimpMesh.Length > 0)
            {
                RenderNode(Root);
            }
        }

        //Ackermann car;
        //float rotWheel;
        //float rotWheelRearLeft;
        //float rotWheelRearRight;

        //public void SetCar(Ackermann ack)
        //{
        //    car = ack;
        //    SteerRotMatrix = SlimDX.Matrix.RotationZ(-car.steeringAngle);

        //    rotWheel += ack.GetWheelAngle(0.41f, ack.distanceWheel / 2.0f);

        //    if (car.steeringAngle > 0)
        //    {
        //        rotWheelRearLeft += ack.GetWheelAngle(0.41f, ack.distanceInnerRear / 2.0f);
        //        rotWheelRearRight += ack.GetWheelAngle(0.41f, ack.distanceOuterRear / 2.0f);
        //    }
        //    else
        //    {
        //        rotWheelRearLeft += ack.GetWheelAngle(0.41f, ack.distanceOuterRear / 2.0f);
        //        rotWheelRearRight += ack.GetWheelAngle(0.41f, ack.distanceInnerRear / 2.0f);
        //    }



        //    RotWheelMatrixFrontLeft = SlimDX.Matrix.RotationY(-rotWheel);
        //    RotWheelMatrixFrontRight = SlimDX.Matrix.RotationY(rotWheel);

        //    RotWheelMatrixRearLeft = SlimDX.Matrix.RotationY(-rotWheelRearLeft);

        //    RotWheelMatrixRearRight = SlimDX.Matrix.RotationY(rotWheelRearRight);
        //}

        public Assimp.Node FrontLeftWheel;
        public Assimp.Node FrontRightWheel;
        public Assimp.Node RearLeftWheel;
        public Assimp.Node RearRightWheel;


        public void RenderNode(Assimp.Node node)
        {
            if (node.HasMeshes)
            {
                foreach (int meshIndex in node.MeshIndices)
                {
                    AssimpMesh[meshIndex].Transform = SlimDX.Matrix.Identity;
                    SlimDX.Matrix transform = SlimDX.Matrix.Identity;

                    Assimp.Node searchNode = node;

                    while (searchNode != null)
                    {
                        SlimDX.Matrix nodeTransform = AssimpAux.ToSlimDXMatrixTransposed(searchNode.Transform);
                        transform = transform * nodeTransform;
                        searchNode = searchNode.Parent;
                    }

                    if (HasAncestor(FrontLeftWheel, node))
                    {
                        AssimpMesh[meshIndex].Transform = RotWheelMatrixFrontLeft * SteerRotMatrix * transform * globalTransform;
                    }
                    else if (HasAncestor(FrontRightWheel, node))
                    {
                        AssimpMesh[meshIndex].Transform = RotWheelMatrixFrontRight * SteerRotMatrix * transform * globalTransform;
                    }
                    else if (HasAncestor(RearLeftWheel, node))
                    {
                        AssimpMesh[meshIndex].Transform = RotWheelMatrixRearLeft * transform * globalTransform;
                    }
                    else if (HasAncestor(RearRightWheel, node))
                    {
                        AssimpMesh[meshIndex].Transform = RotWheelMatrixRearRight * transform * globalTransform;
                    }
                    else
                    {
                        AssimpMesh[meshIndex].Transform = transform * globalTransform;
                    }

                    AssimpMesh[meshIndex].Render();
                }
            }

            if (node.HasChildren)
            {
                foreach (Assimp.Node child in node.Children)
                {
                    RenderNode(child);
                }
            }
        }

        public bool HasAncestor(Assimp.Node foreFather, Assimp.Node node)
        {
            while (node != null)
            {
                if (node == foreFather)
                    return true;

                node = node.Parent;
            }
            return false;
        }

        public override void Dispose()
        {
            foreach (AssimpMesh mesh in AssimpMesh)
            {
                mesh.Dispose();
            }
        }

        public override SlimDX.Matrix Transform
        {
            get
            {
                return AssimpAux.ToSlimDXMatrixTransposed(Root.Transform);
            }
            set
            {
                globalTransform = value;
            }
        }
    }
}
